package com.aaa.controller.system;

import cn.hutool.core.util.ObjectUtil;
import com.aaa.aspect.InsertAnn;
import com.aaa.aspect.UpdateAnn;
import com.aaa.controller.BaseController;
import com.aaa.entity.Result;
import com.aaa.entity.system.LayUiTree;
import com.aaa.entity.system.Menu;
import com.aaa.entity.system.Role;
import com.aaa.service.system.RoleService;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.servlet.ModelAndView;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;

/**
 * 角色信息表(Role)表控制层
 *
 * @author makejava
 * @since 2022-09-06 19:42:17
 */
@RestController
@RequestMapping("role")
public class RoleController extends BaseController {
    /**
     * 服务对象
     */
    @Resource
    private RoleService roleService;


    @RequestMapping("toShowRole.do")
    public ModelAndView toShowMenu(ModelAndView modelAndView) {
        modelAndView.setViewName("system/showRole");
        return modelAndView;
    }

    /**
     * 查询所有角色
     * @return
     */
    @RequestMapping("queryAllRole.do")
    public Result queryAllRole(){
        List<Role> roles = roleService.queryAllRole();
        return success(roles);
    }

    /**
     * 查询所有菜单
     * @return
     */
    @GetMapping("findAllMenu.do")
    public List<LayUiTree> findAllMenu() {
        List<Menu> menus = roleService.findAllMenu();

        List<LayUiTree> list = new ArrayList<>();
        for (Menu menu : menus) {
            if (menu.getParentId() == 0) {
                LayUiTree tree = new LayUiTree();
                tree.setId(menu.getMenuId());
                tree.setTitle(menu.getMenuName());
                tree.setUrl(menu.getUrl());
                tree.setChildren(getChildren(menu.getMenuId(), menus));
                //组件展开
//                tree.setSpread(true);
                tree.setChildren(getChildren(menu.getMenuId(), menus));
                list.add(tree);
            }
        }
        return list;
    }
    
    /**
    * @create by: Username 刘亦辰 
    * @description:  修改用户时， 回显树形菜单
    * @create time: 2022/9/8 16:57
    * @param null  
    * @return :  
    */
    @GetMapping("findSelectMenu.do")
    public List<Integer> findSelectMenu(int roleId) {
        return roleService.findSelectMenu(roleId);
    }


    /**
    * @create by: Username 刘亦辰 
    * @description: 获取对象树的子集目录 
    * @create time: 2022/9/8 16:58
    * @param null  
    * @return :  
    */
    public List<LayUiTree> getChildren(Integer parentId, List<Menu> menus) {
        List<LayUiTree> list = new ArrayList<>();
        for (Menu menu : menus) {
            if (menu.getParentId().equals(parentId)) {
                LayUiTree tree = new LayUiTree();
                tree.setId(menu.getMenuId());
                tree.setTitle(menu.getMenuName());
                tree.setUrl(menu.getUrl());
                //组件展开
//                tree.setSpread(true);
                tree.setChildren(getChildren(menu.getMenuId(), menus));
                list.add(tree);
            }
        }
        return list;
    }


    /**
    * @create by: Username 刘亦辰 
    * @description: 添加菜单用户 
    * @create time: 2022/9/8 17:00
    * @param null
    * @return :  
    */
    @PostMapping("insertRole.do")
    public Result insertRole(Role role, String menus){
        Role insert = roleService.insert(role);
        if (ObjectUtil.isNotEmpty(insert)){
            String[] menusId = menus.split(",");
            roleService.insertRoleMenu(insert.getRoleId(), menusId);
            return success();
        }else {
            return fail();
        }
    }

    /**
    * @create by: Username 刘亦辰 
    * @description: 修改权限 
    * @create time: 2022/9/8 20:01
    * @param null
    * @return :  
    */
    @RequestMapping("updateRole.do")
    public Result updateRole(Role role, String menus) {
        Role update = roleService.update(role);
        if (update != null) {
            String[] menusId = menus.split(",");
            roleService.updateRoleMenu(update.getRoleId(), menusId);
            return success();
        }
        return fail();
    }

    /**
    * @create by: Username 刘亦辰 
    * @description: 删除单个角色 
    * @create time: 2022/9/8 17:19
    * @param null
    * @return :  
    */
    @PostMapping("deleteById.do")
    public Result deleteById(Integer roleId){
        boolean b = roleService.deleteById(roleId);
        if(b){
            return success();
        }else {
            return fail();
        }
    }

}

